var handleAlarmthis;
var dailyAlarmPage = 1;
$(function () {
  // $.ajax({
  //   url: '/paging/notdispose/smoke/pageNumber',
  //   //        async: false,
  //   success: function (result) {
  //     var alarmSum = result.data.总条数;
  //     $('.indexLeft .alarmSum').text(alarmSum);
  //     $(".dataStatistics .dataHandleNum").val(alarmSum);
  //   }
  // });
  //设备异常 打开窗口
  $('.abnormal-trigger').on('click', function (event) {
    event.preventDefault();
    $('.abnormal-pop').addClass('is-visible3');
    $("#abnormalModal select").val("全部")
    abnormalPageList(1, '', 'status=all', '&startTime=2012-12-12 12:12:12', '&endTime=2112-12-12 12:12:12', '');

    abnormalSumPage('', 'status=all', '&startTime=2012-12-12 12:12:12', '&endTime=2112-12-12 12:12:12', '');
    $('#abnormalModal input').val('');
  });
  //设备异常 关闭窗口
  $('.abnormal-pop').on('click', function (event) {
    if ($(event.target).is('.cd-popup-close') || $(event.target).is('.abnormal-pop')) {
      event.preventDefault();
      $(this).removeClass('is-visible3');
    }
  });
  //设备异常 获取总页数 点击分页渲染列表
  function abnormalSumPage(devCode, status, startTime, endTime, handlePeople) {
    var pageSum;
    $.ajax({
      url: '/paging/count/pageNumber?' + status + '&deviceTypeId=1' + startTime + endTime + handlePeople + devCode, //+'&page=1'
      //async: false,
      success: function (result) {
        pageSum = result.data;
        abnormalPageFunc(pageSum, devCode, status, startTime, endTime, handlePeople)
      }
    });
  }

  //设备异常  分页功能
  function abnormalPageFunc(pageSum, devCode, status, startTime, endTime, handlePeople) {
    console.log(pageSum)
    if (pageSum <= 1) {
      $('#abnormalPageBox .pageJump').hide()
    } else {
      $('#abnormalPageBox .pageJump').show()
    }
    Page({
      num: pageSum, //页码数
      startnum: 1, //指定页码
      elem: $('#abnormalPage'), //指定的元素
      callback: function (n) { //回调函数
        console.log(n);
        abnormalPageList(n, devCode, status, startTime, endTime, handlePeople);
      }
    });
  };
  // 设备异常  页面渲染列表
  function abnormalPageList(n, devCode, status, startTime, endTime, handlePeople) {
    $.ajax({
      url: '/paging/count/approve?' + status + '&deviceTypeId=1' + startTime + endTime + handlePeople + devCode + '&page=' + n,
      //	          async: false,
      success: function (result) {

        $('#abnormalModal tbody').html('');
        var data = result.data;
        console.log(data)
        for (var i = 0; i < data.length; i++) {
          //	                  var beginTime = data[i].beginTime;
          var data0 = data[i][0] ? data[i][0] : '';
          var data1 = data[i][1] ? data[i][1] : '';
          var data2 = data[i][2] ? data[i][2] : '';
          var data3 = data[i][3] ? data[i][3] : '';
          var beginTime = data[i][4] ? data[i][4] : '';
          var data5 = data[i][5] ? data[i][5] : '';
          var data6 = data[i][6] ? data[i][6] : '';
          var data7 = data[i][7] ? data[i][7] : '';
          var data8 = data[i][8] ? data[i][8] : '0';
          beginTime = moment(beginTime).format("YYYY-MM-DD HH:mm:ss");
          $('#abnormalModal tbody').append('<tr><td>' + ((+n - 1) * (+8) + (+i) + (+1)) + '</td><td>' + data0 + '</td><td>' + data1 + '</td><td>' + data2 + '</td><td>' + data3 + '</td><td>' + beginTime + '</td><td>' + data5 + '</td><td>' + data6 + '</td><td>' + data7 + '</td><td>' + data8 + '</td></tr>');

        }
      }
    });
  };
  //点击设备异常搜索按钮
  $("#abnormalsearchBtn").on('click', function () {
    var devCode = $("#abnormalModal .devCodeVal").val() ? '&devCode=' + $("#abnormalModal .devCodeVal").val() : '';
    var devType = $("#abnormalModal .devTypeVal").find("option:selected").attr('alarmtype');
    var startTime = $("#abnormalModal .startTImeVal").val() ? $("#abnormalModal .startTImeVal").val() + ' 00:00:00' : '';
    var endTime = $("#abnormalModal .endTimeVal").val() ? $("#abnormalModal .endTimeVal").val() + ' 23:59:59' : '';
    var handlePeople = $("#abnormalModal .handlePeopleVal").val() ? '&disposePeople=' + $("#abnormalModal .handlePeopleVal").val() : '';
    //		  devCode,devType,startTIme,endTime,handlePeople
    var status = 'status=' + devType;
    startTime = startTime ? '&startTime=' + startTime : '&startTime=2012-12-12 12:12:12';
    endTime = endTime ? '&endTime=' + endTime : '&endTime=2122-12-12 23:59:59';

    console.log(devCode, devType, startTime, endTime, handlePeople)
    abnormalSumPage(devCode, status, startTime, endTime, handlePeople);
    abnormalPageList(1, devCode, status, startTime, endTime, handlePeople);
    $('.pageJump input').val('');
  });

  //----------------------------------------------
  //报警管理
  //打开窗口
  $('.cd-popup-trigger3').on('click', function (event) {
    dailyAlarmPage = 1;
    event.preventDefault();
    $('.daily-alarm-popup').addClass('is-visible3');
    // 报警管理 获取总页数 点击分页渲染列表
    dailyAlarmSumPage(dailyAlarmPage);
    // 报警管理 首次渲染
    dailyAlarmPageList(dailyAlarmPage);
    $('.pageJump input').val('');
  });
  //关闭窗口
  $('.daily-alarm-popup').on('click', function (event) {
    if ($(event.target).is('.cd-popup-close') || $(event.target).is('.daily-alarm-popup')) {
      event.preventDefault();
      $(this).removeClass('is-visible3');
    }
    if ($(event.target).is('.popup-close') || $(event.target).is('.daily-alarm-popup')) {
      event.preventDefault();
      $(this).removeClass('is-visible3');
    }

  });

  //报警管理 处理
  var devCodeHandle, alarmIdHandle, disposeStatusHandle;
  $('#dailyAlarmModal .manageModal .tableModal').on('click', '.handleBtn', function () {
    var $thisParent = $(this).parent();
    handleAlarmthis = $thisParent;
    devCodeHandle = $thisParent.attr('devcode');
    alarmIdHandle = $thisParent.attr('alarmid');
    disposeStatusHandle = 1;
  });

  //报警管理 忽略
  $('#dailyAlarmModal .manageModal .tableModal').on('click', '.ignoreBtn', function () {
    var $thisParent = $(this).parent();
    handleAlarmthis = $thisParent;
    devCodeHandle = $thisParent.attr('devcode');
    alarmIdHandle = $thisParent.attr('alarmid');
    disposeStatusHandle = 2;
  });

  //  报警管理 处理和忽略封装
  function dailyAlarmHandle(devCode, alarmId, disposeStatus) {
    $.ajax({
      url: '/alarm/disposealarm?devCode=' + devCode + '&alarmId=' + alarmId + '&disposeStatus=' + disposeStatus,

      success: function (result) {
        console.log(result)
        $('.alarmHandle-pop').removeClass('is-visible3');
        $('.alarmIgnore-pop').removeClass('is-visible3');
      }
    });
  }

  //  报警管理 打开确认处理窗口
  $('body').on('click', '.alarmHandle-trigger', function (event) {
    event.preventDefault();
    $('.alarmHandle-pop').addClass('is-visible3');
  });
  //报警管理  关闭确认处理窗口
  $('.close-pop').on('click', function (event) {
    $('.alarmHandle-pop').removeClass('is-visible3');
    $('.alarmIgnore-pop').removeClass('is-visible3');
  });
  //  报警管理  处理和忽略  点击确认按钮
  $('.alarmHandleIgnore .alarmHandleBtn').on('click', function () {
    dailyAlarmHandle(devCodeHandle, alarmIdHandle, disposeStatusHandle);
    // 报警管理 首次渲染
    dailyAlarmPageList(dailyAlarmPage);
    // 报警管理 获取总页数 点击分页渲染列表
    dailyAlarmSumPage(dailyAlarmPage);
    $('.pageJump input').val('');
    alarmSumInitFunc();
    //停止声音
    stopSound();
    // smokeAlarmDataAjax();
      newAlarmFn();
    alarmpageTurningBtn();
    smokeAlarmRender(alarmHandleNum);
    //        if ($('#dailyAlarmModal tbody tr').length == 1 && dailyPageSum == 1) {
    //            $('.daily-alarm-popup').removeClass('is-visible3');
    //        }
  });
  //  报警管理忽略
  $('body').on('click', '.alarmIgnore-trigger', function (event) {
    event.preventDefault();
    $('.alarmIgnore-pop').addClass('is-visible3');
  });
  //  报警管理 点击定位定位到地图
  $('#dailyAlarmModal .tableModal tbody').on('click', '.positionPonitMap', function () {
    var $thisParent = $(this).parent();
    var longitude = $thisParent.attr('longitude');
    var latitude = $thisParent.attr('latitude');
    var devCode = $thisParent.attr('devCode');
    BaiDuMap.panTo(latitude, longitude);
    setTimeout(function () {
      BaiDuMap.popMenu(devCode);
    }, 500);
  });

  //-----------------------------------------------------------------------------
  //历史信息 打开窗口
  $('.historyNews-trigger').on('click', function (event) {
    event.preventDefault();
    $('.historyNews-pop').addClass('is-visible3');
    $("#historyNewsModal select").val("全部")
    historyNewsPageList(1, '', 'status=all', '&beginTime=2012-12-12 12:12:12', '&endTime=2112-12-12 12:12:12', '', '&alarmTypeCode=001');

    historyNewsSumPage('', 'status=all', '&beginTime=2012-12-12 12:12:12', '&endTime=2112-12-12 12:12:12', '', '&alarmTypeCode=001');
    $('#historyNewsModal input').val('');
  });
  //历史信息 关闭窗口
  $('.historyNews-pop').on('click', function (event) {
    if ($(event.target).is('.cd-popup-close') || $(event.target).is('.historyNews-pop')) {
      event.preventDefault();
      $(this).removeClass('is-visible3');
    }
  });

  //点击历史信息搜索按钮
  $("#historyNewssearchBtn").on('click', function () {
    var devCode = $("#historyNewsModal .devCodeVal").val() ? '&devCode=' + $("#historyNewsModal .devCodeVal").val() : '';
    var devType = $("#historyNewsModal .devTypeVal").find("option:selected").attr('alarmtype');
    var startTime = $("#historyNewsModal .startTImeVal").val() ? $("#historyNewsModal .startTImeVal").val() + ' 00:00:00' : '';
    var endTime = $("#historyNewsModal .endTimeVal").val() ? $("#historyNewsModal .endTimeVal").val() + ' 23:59:59' : '';
    var handlePeople = $("#historyNewsModal .handlePeopleVal").val() ? '&disposePeople=' + $("#historyNewsModal .handlePeopleVal").val() : '';
    var alarmTypeCode = $("#historyNewsModal .alarmReasonsVal").find("option:selected").attr('alarmTypeCode') ? '&alarmTypeCode=' + $("#historyNewsModal .alarmReasonsVal").find("option:selected").attr('alarmTypeCode') : '';

    var status = 'status=' + devType;
    startTime = startTime ? '&beginTime=' + startTime : '&beginTime=2012-12-12 12:12:12';
    endTime = endTime ? '&endTime=' + endTime : '&endTime=2122-12-12 23:59:59';

    console.log(devCode, devType, startTime, endTime, handlePeople, alarmTypeCode)
    historyNewsSumPage(devCode, status, startTime, endTime, handlePeople, alarmTypeCode);
    historyNewsPageList(1, devCode, status, startTime, endTime, handlePeople, alarmTypeCode);
    $('.pageJump input').val('');
  });

  // 报警历史信息 操作 定位
  $("#historyNewsModal .tableBox tbody").on("click", ".positionBtn" , function(){
    console.log("111");
    var $this = $(this);
    // 获取经度
    var longitude = $this.attr('longitude');
    // var longitude = 120.2;
    // 获取纬度
    var latitude = $this.attr('latitude');
    // var latitude = 30.3;
    var devCode = $this.attr('devCode');
    // 120.2E ,30.3N
    $(".historyNews-pop").removeClass("is-visible3");
    BaiDuMap.panTo(latitude, longitude);
    setTimeout(function () {
      BaiDuMap.popMenu(devCode);
    }, 700);
  })
  // c4cd82632c67306
});
// 点击地图烟感点位上报警历史按钮，展示报警点位历史信息
function smokeSeeHistoryFunc() {
  $("body .smokeSeeHistory").on("click", function (event) {
      var devCode = $(this).attr('no');
      if (devCode) {
        devCode = '&devCode=' + devCode;
      } else {
        devCode = '';
      }
      event.preventDefault();
      $('.historyNews-pop').addClass('is-visible3');
      $("#historyNewsModal select").val("全部")
      historyNewsPageList(1, devCode, 'status=all', '&beginTime=2012-12-12 12:12:12', '&endTime=2112-12-12 12:12:12', '', '&alarmTypeCode=001');

      historyNewsSumPage(devCode, 'status=all', '&beginTime=2012-12-12 12:12:12', '&endTime=2112-12-12 12:12:12', '', '&alarmTypeCode=001');
      $('#historyNewsModal input').val('');
    });
  }
  //历史信息 获取总页数 点击分页渲染列表
  function historyNewsSumPage(devCode, status, startTime, endTime, handlePeople, alarmTypeCode) {
    var pageSum;
    // 应答器
    var deviceTypeId = '001';
    console.log(devCode,devCode==1001||devCode==1002)
    if(devCode=='&devCode=1001'||devCode=='&devCode=1002'){
      deviceTypeId = '009';
      alarmTypeCode = '&alarmTypeCode=004';
    }
    $.ajax({
      url: '/paging/data/history/pageNumber?' + startTime + alarmTypeCode + endTime + '&deviceTypeId='+deviceTypeId+devCode,
      //              async: false,
      success: function (result) {
        console.log(result)
        alarmTypeCode = '&alarmTypeCode=001';
        pageSum = +result.data.报警历史总页数;
        var historyNewsHandleSum = +result.data.报警历史总条数;
        var historyNewsUnHandleSum = +result.data.未处理报警数;
        var historyNewsUnHandleNum = historyNewsHandleSum - historyNewsUnHandleSum;
        //            	  console.log(historyNewsHandleSum,historyNewsUnHandleSum,historyNewsHandleNum)
        $('#historyNewsModal .historyNewsHandleNum').text(historyNewsUnHandleNum);
        $('#historyNewsModal .historyNewsUnHandleNum').text(historyNewsUnHandleSum);
        historyNewsPageFunc(pageSum, devCode, status, startTime, endTime, handlePeople, alarmTypeCode);
      }
    });
  }

  //历史信息  分页功能
  function historyNewsPageFunc(pageSum, devCode, status, startTime, endTime, handlePeople, alarmTypeCode) {
    console.log(pageSum)
    if (pageSum <= 1) {
      $('#historyNewsPageBox .pageJump').hide();
    } else {
      $('#historyNewsPageBox .pageJump').show();
    }
    Page({
      num: pageSum, //页码数
      startnum: 1, //指定页码
      elem: $('#historyNewsPage'), //指定的元素
      callback: function (n) { //回调函数
        console.log(n);
        historyNewsPageList(n, devCode, status, startTime, endTime, handlePeople, alarmTypeCode);
      }
    });
  };
  // 历史信息  页面渲染列表
  function historyNewsPageList(n, devCode, status, startTime, endTime, handlePeople, alarmTypeCode) {
    // 应答器
    var deviceTypeId = '001';
    if(devCode=='&devCode=1001'||devCode=='&devCode=1002'){
      deviceTypeId = '009';
      alarmTypeCode = '&alarmTypeCode=004';
    }
    $.ajax({
      url: '/paging/data/history/Approve?' + startTime + endTime + alarmTypeCode + devCode+'&deviceTypeId='+deviceTypeId+'&page=' + n, //&alarmTypeCode=火警
      //              async: false,
      success: function (result) {
        $('#historyNewsModal tbody').html('');
        var data = result.data;
        console.log(data);
        alarmTypeCode = '&alarmTypeCode=001';
        for (var i = 0; i < data.length; i++) {
          //                    var beginTime = data[i].beginTime;
          var data0 = data[i][0] ? data[i][0] : '';
          var data1 = data[i][1] ? data[i][1] : '';
          var data2 = data[i][2] ? data[i][2] : '';
          var data3 = data[i][3] ? data[i][3] : '';
          var data4 = data[i][5] ? data[i][4] : '';
          var beginTime = data[i][5];
          var data6 = data[i][6] ? data[i][6] : '';
          var data8 = data[i][8] ? data[i][8] : '';
          beginTime = beginTime ? moment(beginTime).format("YYYY-MM-DD HH:mm:ss") : '';
          var latitude = data[i][10];
          var longitude = data[i][9];
          $('#historyNewsModal tbody').append('<tr><td>' + ((+n - 1) * (+8) + (+i) + (+1)) + '</td><td>' + data0 + '</td><td>' + data1 + '</td><td>' + data2 + '</td><td>' + data3 + '</td><td>' + data4 + '</td><td>' + beginTime + '</td><td>' + data6 + '</td><td>' + data8 + '</td><td><span class="positionBtn" longitude ="'+longitude+'" latitude="'+latitude+'"  devCode="'+data0+'">定位</span></td></tr>');

        }
      }
    });
  };
  // ------------------------

  //报警管理  页面渲染列表
  function dailyAlarmPageList(n) {
    console.log(n)
    $.ajax({
      url: '/paging/notdispose/smoke/approve/' + n,
      //        async: false,
      success: function (result) {
        $('#dailyAlarmModal tbody').html('');
        dailyAlarmPage = n;
        var data = result.data;
        console.log(data)
        if (data.length != 0) {
          for (var i = 0; i < data.length; i++) {
            var beginTime = data[i][5];
            beginTime = moment(beginTime).format("YYYY-MM-DD HH:mm:ss");
            var place = data[i][2] ? data[i][2] : '';
            $('#dailyAlarmModal tbody').append('<tr><td>' + ((+n - 1) * (+8) + (+i) + (+1)) + '</td><td>' + data[i][0] + '</td><td>' + data[i][1] + '</td><td>' + place + '</td><td>' + data[i][3] + '</td><td>' + data[i][4] + '</td><td>' + beginTime + '</td><td devCode="' + data[i][0] + '" alarmId="' + data[i][6] + '" longitude="' + data[i][7] + '" latitude="' + data[i][8] + '"><span class="handleBtnStyle mr5 handleBtn alarmHandle-trigger">处理</span><span class="handleBtnStyle mr5 ignoreBtn alarmIgnore-trigger">忽略</span><span class="handleBtnStyle positionPonitMap popup-close">定位</span></td></tr>')
          }
        } else {
          if (dailyAlarmPage > 1) {
            dailyAlarmPage = dailyAlarmPage - 1;
          } else {
            return false;
          }

          // 报警管理 首次渲染
          dailyAlarmPageList(dailyAlarmPage);
          // 报警管理 获取总页数 点击分页渲染列表
          dailyAlarmSumPage(dailyAlarmPage);
          $('.pageJump input').val('');
        }
      }
    });
  };

  //报警管理 获取总页数 点击分页渲染列表
  var dailyPageSum;

  function dailyAlarmSumPage(dailyAlarmPage) {
    var dailyAlarmPageSum;
    $.ajax({
      url: '/paging/notdispose/smoke/pageNumber',
      //        async: false,
      success: function (result) {
        console.log(dailyAlarmPage)
        dailyAlarmPageSum = result.data.总页数;
        dailyPageSum = dailyAlarmPageSum;
        var alarmSum = result.data.总条数;
        $('#dailyAlarmModal .alarmSum').text(alarmSum);
        $('.indexLeft .alarmSum').text(alarmSum);
        dailyAlarmPageFunc(dailyAlarmPageSum, dailyAlarmPage)
      }
    });
  }

  //报警管理  分页功能
  function dailyAlarmPageFunc(pageSum, dailyAlarmPage) {
    console.log(pageSum)
    if (pageSum <= 1) {
      $('#dailyAlarmPageBox .pageJump').hide();
    } else {
      $('#dailyAlarmPageBox .pageJump').show();
    }
    Page({
      num: pageSum, //页码数
      startnum: dailyAlarmPage, //指定页码
      elem: $('#dailyPage'), //指定的元素
      callback: function (n) { //回调函数
        console.log(n);
        dailyAlarmPageList(n);
      }
    });
  };

 
  